/*	HeatmapViewFrame.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of MSGToolSuite_Swing ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.gui.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import edu.bu.manateem.msg.MSGConstants;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.JSVGScrollPane;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		May 26, 2010	- Version 1.0.
 */
public class HeatmapViewFrame
	extends JFrame
	implements ViewFrame
{
	/*
	 * TYPES
	 */

	/*
	 * CONSTANTS
	 */

	// Serialize
	private static final long serialVersionUID = 1L;
	// Logging
	private static final Logger LOG =
		LoggerFactory.getLogger(HeatmapViewFrame.class);
	
	// Layout properties
	private static final int FRAME_DEFAULT_WIDTH =
		MSGConstants.SCREEN_SIZE.width / 2;
	private static final int FRAME_DEFAULT_HEIGHT =
		MSGConstants.SCREEN_SIZE.height / 2;
	private static final int FRAME_DEFAULT_OFFSET_X =
		MSGConstants.SCREEN_SIZE.width / 4;
	private static final int FRAME_DEFAULT_OFFSET_Y =
		MSGConstants.SCREEN_SIZE.height / 10;
	private static final Dimension FRAME_DEFAULT_SIZE =
		new Dimension(HeatmapViewFrame.FRAME_DEFAULT_WIDTH,
			HeatmapViewFrame.FRAME_DEFAULT_HEIGHT);
	private static final Point FRAME_DEFAULT_OFFSET =
		new Point(HeatmapViewFrame.FRAME_DEFAULT_OFFSET_X,
			HeatmapViewFrame.FRAME_DEFAULT_OFFSET_Y);
	
	private static final int X_SPACE_BETWEEN_COMPONENTS = 10;
	private static final int Y_SPACE_BETWEEN_COMPONENTS = 10;
	@SuppressWarnings("unused")
	private static final Dimension SPACE_BETWEEN_COMPONENTS =
		new Dimension(HeatmapViewFrame.X_SPACE_BETWEEN_COMPONENTS,
			HeatmapViewFrame.Y_SPACE_BETWEEN_COMPONENTS);
	
	@SuppressWarnings("unused")
	private static final int BORDER_SIZE_TOP = 15;
	private static final int BORDER_SIZE_BOTTOM = 5;
	private static final int BORDER_SIZE_LEFT = 10;
	private static final int BORDER_SIZE_RIGHT = 10;
	
	/*
	 * FIELDS
	 */

	private JSVGCanvas svgCanvas = new JSVGCanvas();
	private JToolBar toolBar;
	private boolean isEmptyView = true;
	
	/*
	 * CONSTRUCTORS
	 */

	public HeatmapViewFrame()
	{
		super();
		
		HeatmapViewFrame.LOG.debug("init self");
		
		initSVGCanvas();
		initTools();
		init();
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	public static void main(String[] args)
	{
		new HeatmapViewFrame();
	}
	
	/**
	 *	<p>
	 *	
	 *	</p>
	 *
	 *	@since
	 *		
	 */
	@Override
	public void disposeView()
	{
		dispose();
		
	}
	
	private void init()
	{
		HeatmapViewFrame.LOG.debug("build container");
		
		// behaviors
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Heatmap Viewer");
		setVisible(true);
		
		// layout
		final JSVGScrollPane masterPane = new JSVGScrollPane(svgCanvas);
		masterPane.setAutoscrolls(true);
		masterPane.addMouseWheelListener(new MouseWheelListener()
		{
			@Override
			public void mouseWheelMoved(MouseWheelEvent e)
			{
				
				int notches = e.getWheelRotation();
				System.out.println(notches);
				masterPane.scrollRectToVisible(new Rectangle(new Dimension(100,
					50)));
				// masterPane.scaleChange(new Integer(notches).floatValue());
			}
		});
		
		setLayout(new BorderLayout());
		setLocation(HeatmapViewFrame.FRAME_DEFAULT_OFFSET);
		setPreferredSize(HeatmapViewFrame.FRAME_DEFAULT_SIZE);
		
		// finish
		getContentPane().add(masterPane,BorderLayout.CENTER);
		getContentPane().add(toolBar,BorderLayout.PAGE_END);
		pack();
		
		HeatmapViewFrame.LOG.debug("build container -- DONE");
	}
	
	private void initSVGCanvas()
	{
		// build SVG canvas
		svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter()
		{
			@Override
			public void gvtBuildCompleted(GVTTreeBuilderEvent e)
			{
				setTitle("Built.");
				pack();
			}
			
			@Override
			public void gvtBuildStarted(GVTTreeBuilderEvent e)
			{
				setTitle("Building SVG machine...");
			}
		});
		svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter()
		{
			@Override
			public void gvtRenderingCompleted(GVTTreeRendererEvent e)
			{
				setTitle("Heatmap Viewer");
			}
			
			@Override
			public void gvtRenderingPrepare(GVTTreeRendererEvent e)
			{
				setTitle("Rendering SVG heatmap...");
			}
		});
		svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter()
		{
			@Override
			public void documentLoadingCompleted(SVGDocumentLoaderEvent e)
			{
				setTitle("Loaded.");
			}
			
			@Override
			public void documentLoadingStarted(SVGDocumentLoaderEvent e)
			{
				setTitle("Loading SVG document...");
			}
		});
		// svgCanvas.setEnableImageZoomInteractor(true);
		// svgCanvas.
		
		svgCanvas.setDisableInteractions(false);
		svgCanvas.setEnableZoomInteractor(true);
		System.out.println(JSVGCanvas.ZOOM_IN_ACTION);
		// JSVGCanvas.ZoomAction za = svgCanvas.new ZoomAction(10);
		
		// render SVG canvas
		JFileChooser fc = new JFileChooser(".");
		int choice = fc.showOpenDialog(this);
		if (choice == JFileChooser.APPROVE_OPTION)
		{
			svgCanvas.setURI(fc.getSelectedFile().toURI().toString());
			isEmptyView = false;
		}
	}
	
	private void initTools()
	{
		// build TODO
		HeatmapViewFrame.LOG.debug("build load button");
		HeatmapViewFrame.LOG.debug("build load button -- DONE");
		
		// build tool bar
		HeatmapViewFrame.LOG.debug("build tool bar");
		toolBar = new JToolBar("HEatmap Toolbar",SwingConstants.HORIZONTAL);
		toolBar.setAlignmentX(Component.LEFT_ALIGNMENT);
		toolBar.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		toolBar.setBorder(BorderFactory.createEmptyBorder(
			HeatmapViewFrame.Y_SPACE_BETWEEN_COMPONENTS,
			HeatmapViewFrame.BORDER_SIZE_LEFT,
			HeatmapViewFrame.BORDER_SIZE_BOTTOM,
			HeatmapViewFrame.BORDER_SIZE_RIGHT));
		toolBar.setLayout(new GridBagLayout());
		toolBar.setMaximumSize(new Dimension(MSGConstants.SCREEN_SIZE.width,25));
		
		HeatmapViewFrame.LOG.debug("build tool bar -- DONE");
	}
	
	/**
	 *	<p>
	 *	
	 *	</p>
	 *
	 *	@since
	 *		
	 */
	@Override
	public boolean isEmptyView()
	{
		return isEmptyView;
	}
}
